/***********************************************
 This file is part of redia-library-app-ios.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#import "NSString+LibUtils.h"


@implementation NSString (LibUtils)

+ (BOOL)isContained:(NSString*)s1 inString:(NSString*)s2
{
	NSRange index = [s2 rangeOfString:s1];
	return (index.location!=NSNotFound);
}

- (BOOL)isContaining:(NSString*)substring
{
	return [NSString isContained:substring inString:self];
}

+ (NSString*)unescapeFromXMLString:(NSString*)string 
{
	if (string==nil) {
		return nil;
	}
	NSMutableString* output_string = [NSMutableString stringWithString:string];
	[output_string replaceOccurrencesOfString:@"&lt;" withString:@"<" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@"&gt;" withString:@">" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@"&quot;" withString:@"\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@"&amp;" withString:@"&" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@"&apos;" withString:@"'" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	
	return [output_string decodedNumericHTMLEntities];
}

- (NSString*)unescapedFromXML
{
    return [NSString unescapeFromXMLString:self];
}

+ (NSString*)escapeToXMLString:(NSString*)string 
{
	if (string==nil) {
		return nil;
	}
	NSMutableString* output_string = [NSMutableString stringWithString:string];
	[output_string replaceOccurrencesOfString:@"&"  withString:@"&amp;" 	options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@"<"  withString:@"&lt;"  	options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@">"  withString:@"&gt;"  	options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@"\"" withString:@"&quot;"	options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	[output_string replaceOccurrencesOfString:@"'"  withString:@"&apos;"	options:NSCaseInsensitiveSearch range:NSMakeRange(0, [output_string length])];
	
	return output_string;
}

- (NSString*)escapedToXML
{
    return [NSString escapeToXMLString:self];
}

+ (NSString*)decodeNumericHTMLEntities:(NSString*)sourceString
{
    NSMutableString *resultString = [sourceString mutableCopy];
    NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"&#x([\\da-f][\\da-f][\\da-f]*);" options:NSRegularExpressionCaseInsensitive error:nil];
    NSInteger offset = 0; // Offset generated by prior text replacements.
    
    for (NSTextCheckingResult *result in [regexp matchesInString:sourceString
                                                         options:0
                                                           range:NSMakeRange(0, [sourceString length])]) {
        NSRange resultRange = [result range];
        resultRange.location += offset;
        
        NSString *match = [regexp replacementStringForResult:result
                                                    inString:resultString
                                                      offset:offset
                                                    template:@"$1"];
        NSScanner *scanner = [NSScanner scannerWithString:match];
        unsigned character;
        if ([scanner scanHexInt:&character] == NO) {
            NSLog(@"Error converting entity number to decimal.");
            return nil;
        }
        NSString *replacement = [NSString stringWithCharacters:(unichar*)&character length:1];
        [resultString replaceCharactersInRange:resultRange withString:replacement];
        
        offset -= [match length] + 3;
    }
    
    return resultString;
}

- (NSString*)decodedNumericHTMLEntities
{
    return [NSString decodeNumericHTMLEntities:self];
}

+ (UIColor*)getColorFromHexString:(NSString*)hexstring withDefaultColor:(UIColor*)defaultcolor {
    if ([hexstring length]==6) {
        NSScanner* hexscanner = [NSScanner scannerWithString:hexstring];
        unsigned int col_num;
        BOOL success = [hexscanner scanHexInt:&col_num];
        if (success) {
            unsigned char r = (col_num >> 16) & 0xff;
            unsigned char g = (col_num >> 8) & 0xff;
            unsigned char b = (col_num) & 0xff;
            UIColor* colorInstance = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
            return colorInstance;
        }
    } else {
        NSLog(@"Warning: color not decodable");
    }
    return defaultcolor;
}

- (UIColor*)getColorFromHexStringWithDefaultColor:(UIColor*)defaultcolor {
    return [NSString getColorFromHexString:self withDefaultColor:defaultcolor];
}

@end
